package com.enlighten.coinDataFetcher;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ConcentricCircleDrawerAlter {

	private int radius;
	private int squaredRadius;
	private Color currentColor;
	private int currentRadius;
	private BufferedImage bufferedImage;

	public ConcentricCircleDrawerAlter(int radius) {
		this.radius = radius;
		if ( radius % 2 == 0 )
			this.radius++;
		squaredRadius = radius*radius;
		int imgSize = getImageSize();
		bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
	}

	public static void main(String[] args) {
		ConcentricCircleDrawerAlter circleDrawer = new ConcentricCircleDrawerAlter(100);
		Color currentColor = new Color(20,20,20);
		for(int i = 10; i < 101 ; i++){
			circleDrawer.setCurrentRadiusAndColor(i,currentColor);
			circleDrawer.draw("");
			if ( currentColor.getRed() < 255 ){
				currentColor = new Color(currentColor.getRed() + 10 > 255 ? 255 :currentColor.getRed() + 10, currentColor.getGreen(), currentColor.getBlue());
			}else{
				if ( currentColor.getGreen() < 255 ){
					currentColor = new Color(currentColor.getRed(), currentColor.getGreen() + 10 > 255 ? 255 :currentColor.getGreen() + 10, currentColor.getBlue());
				}else{
					if ( currentColor.getBlue() < 255 ){
						currentColor = new Color(currentColor.getRed(), currentColor.getGreen(), currentColor.getBlue() + 10 > 255 ? 255 :currentColor.getBlue() + 10);
					}
				}
			}
			/*
			if ( currentColor == Color.gray )
				currentColor = Color.blue;
			if ( currentColor == Color.orange )
				currentColor = Color.gray;
			if ( currentColor == Color.green )
				currentColor = Color.orange;
			if ( currentColor == Color.blue )
				currentColor = Color.green;
			*/
		}
		circleDrawer.saveImage("experiment.png");
		
	}

	private void setCurrentRadiusAndColor(int radius, Color currentColor) {
		currentRadius = radius;
		this.currentColor = currentColor;
	}

	private void draw(String filename) {
		int imgSize = getImageSize();
		for(int i=0; i < imgSize ; i++)
			for(int j=0; j < imgSize ; j++){
				int ni = i - radius + currentRadius;
				int nj = j - radius + currentRadius;
				Color imageColor = new Color(bufferedImage.getRGB(i, j));
				Color newColor = new Color(getColorFor(currentRadius,ni,nj));
				bufferedImage.setRGB(i, j, mergeColors(imageColor,newColor));
			}
	}

	private void saveImage(String filename) {
		try {
			ImageIO.write(bufferedImage, "PNG", new File(filename));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private int mergeColors(Color imageColor, Color newColor) {
//		if (  newColor.equals(Color.white) || newColor.equals(Color.black))
//			return imageColor.getRGB();
//		
//		if ( imageColor.equals(Color.black) || imageColor.equals(Color.white) ){
//			return newColor.getRGB();
//		}else
//			return clamp((imageColor.getRed() + newColor.getRed())/2,
//					(imageColor.getBlue() + newColor.getBlue())/2,
//					(imageColor.getGreen() + newColor.getGreen())/2);
		// Image was not painted, paint it
		if ( imageColor.equals(Color.black) )
			return newColor.getRGB();
		
		// Image was painted
		// If we have to paint again, use green
		if ( !newColor.equals(Color.black) )
			return Color.GREEN.getRGB();
		
		return imageColor.getRGB();
	}

	private int clamp(int red, int blue, int green) {
		if ( red > 255 )
			red = 255;
		if ( blue > 255 )
			blue = 255;
		if ( green > 255 )
			green = 255;
		
		return new Color(red, green, blue).getRGB();
	}

	private int getColorFor(int currentRadius, int i, int j) {
		int translatedX = i - currentRadius;
		int translatedY = j - currentRadius;
		int squaredDistance = translatedX*translatedX + translatedY*translatedY;
		if ( shouldPaint(squaredDistance, currentRadius*currentRadius, currentRadius) )
			return currentColor.getRGB();
		return Color.BLACK.getRGB();
	}

	private boolean shouldPaint(int squaredDistance, int squaredRadius, int currentRadius) {
//		return (squaredDistance > squaredRadius - radius + 1) && (squaredDistance < squaredRadius + radius + 1);
		return (squaredDistance > squaredRadius - currentRadius + 1) && (squaredDistance < squaredRadius + currentRadius + 1);
//		return (squaredDistance > squaredRadius - currentRadius + 1/4) && (squaredDistance < squaredRadius + currentRadius + 1/4);
	}

	private int getImageSize() {
		return radius*2+1;
	}
}
